package com.gues.ddc_app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gues.ddc_app.entity.Admin;
import com.gues.ddc_app.entity.Feedback;
import com.gues.ddc_app.entity.ResultData;
import com.gues.ddc_app.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huHang
 * @since 2023-02-12
 */
@Controller
@CrossOrigin//跨域
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;
    @RequestMapping("/login")
    @ResponseBody
    public Admin login(String admin1, HttpSession session) throws JsonProcessingException {
        ObjectMapper jsonTool = new ObjectMapper();
        Admin admin=jsonTool.readValue(admin1,Admin.class);
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("admin_name",admin.getAdminName());
        wrapper.eq("admin_password",admin.getAdminPassword());
        Admin admin2=adminService.getOne(wrapper);
        if(admin2!=null){
            session.setAttribute("admin",admin2);
            return admin2;
        }else {
            return null;
        }
    }
    @RequestMapping("/upPassword")
    @ResponseBody
    public boolean upPassword(Admin admin,HttpSession session){
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("admin_name",admin.getAdminName());
        boolean b1=adminService.update(admin,wrapper);
        session.setAttribute("admin",admin);
        return b1;
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("admin");
        return "redirect:http://localhost:8080/";
    }
}
